package cn.esbot.wx.mp.handler;

import cn.esbot.wx.mp.builder.ImageBuilder;
import cn.esbot.wx.mp.builder.TextBuilder;
import cn.esbot.wx.mp.builder.VideoBuilder;
import cn.esbot.wx.mp.client.EsbotTemplate;
import cn.esbot.wx.mp.config.EsbotProperties;
import cn.esbot.wx.mp.utils.FileUtils;
import cn.esbot.wx.mp.utils.JsoupUtils;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.Map;


@Component
@AllArgsConstructor
public class MsgHandler extends AbstractHandler {
    private EsbotTemplate esbotTemplate;
    private EsbotProperties esbotProperties;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {

        WxMpUser wxMpUser = new WxMpUser();
        String fromUser = wxMessage.getFromUser();
        try {
            wxMpUser = weixinService.getUserService().userInfo(fromUser);
        } catch (WxErrorException e) {
            logger.error("获取用户信息异常：", e);
        }
        String username = wxMpUser.getNickname();
        String region = wxMpUser.getProvince() + "省" + wxMpUser.getCity() + "市";
        String messageContent = wxMessage.getContent();
        String content = esbotTemplate.chat(messageContent, fromUser, username, region);
        try {
            List<String> imageLink = JsoupUtils.extraImageLink(content);
            if (!imageLink.isEmpty()) {
                File file = FileUtils.downloadFile(imageLink.get(0));
                if (file.length() < 2 * 1024 * 1024) {
                    WxMediaUploadResult result =
                            weixinService.getMaterialService().mediaUpload(WxConsts.MediaFileType.IMAGE, file);
                    String mediaId = result.getMediaId();
                    return new ImageBuilder().build(mediaId, wxMessage, weixinService);
                }
            }
            List<String> videoLink = JsoupUtils.extraVideoLink(content);
            if (!videoLink.isEmpty()) {
                File file = FileUtils.downloadFile(imageLink.get(0));
                WxMediaUploadResult result =
                        weixinService.getMaterialService().mediaUpload(WxConsts.MediaFileType.VIDEO, file);
                String mediaId = result.getMediaId();
                return new VideoBuilder().build(mediaId, wxMessage, weixinService);
            }
        } catch (Exception ignored) {
        }
        return new TextBuilder().build(content, wxMessage, weixinService);

    }

}
